package altocloud.ehcache.test;

import org.hibernate.*;
import org.hibernate.cfg.*;

import altocloud.hibernate.StationInfo;
import altocloud.ehcache.test.TimeMeasurement;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.Test;

public class testEhcache {
	@Test
	public void testSessionCache() throws Exception {
		Session session = null;
		TimeMeasurement duration = new TimeMeasurement(TimeUnit.MILLISECONDS);

		try {

			SessionFactory sessionFactory = new Configuration().configure()
					.buildSessionFactory();
			session = sessionFactory.openSession();

			for (int i = 0; i < 20; i++) {
				Criteria crit = session.createCriteria(StationInfo.class);
				crit.setCacheable(true);

				duration.startMeasurement();
				List list = crit.list();
				duration.endMeasurement();

				System.out.println("Test Session Cache: Time to FIND "
						+ list.size() + " Station Infos:" + duration.getTime()
						+ " msec");

			}
			session.close();

		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			Date end_date = new Date();

		}
	}

	@Test
	public void testEHCache() throws Exception {
		Session session = null;
		TimeMeasurement duration = new TimeMeasurement(TimeUnit.MILLISECONDS);

		try {

			SessionFactory sessionFactory = new Configuration().configure()
					.buildSessionFactory();

			for (int i = 0; i < 20; i++) {
				session = sessionFactory.openSession();
				Criteria crit = session.createCriteria(StationInfo.class);
				crit.setCacheable(true);

				duration.startMeasurement();
				List list = crit.list();
				duration.endMeasurement();

				System.out.println("Test EHCache: Time to FIND " + list.size()
						+ " Station Infos:" + duration.getTime() + " msec");
				session.close();
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			Date end_date = new Date();

		}
	}

}
